var Snack = require('../models/snack');
var express = require('express');
var router = express.Router();
var fs = require('fs'); 
var uuid = require('node-uuid');

router.route('/snack')
    .post(function(req, res) {
       var picUUID = uuid.v1();
       var arr = [];
       var array = req.body.pic;
       for(var x in array){
         arr.push(array[x]);
       }
       var buffer = new Buffer(arr);
        fs.writeFile("./public/images/"+picUUID+".png", buffer,function(err){
        	if(err){
        		console.log('upload error');
        	}else{
        		console.log('upload success')
        	}
        	
        });  
        var snack = new Snack();      // create a new instance of the Bear model
        snack.pic = "./images/"+picUUID+".png"; 
        snack.loves = req.body.loves;
        snack.description = req.body.description; // set the bears name (comes from the request)
        snack.category = req.body.category;
        snack.name = req.body.name;
        snack.price = req.body.price;
        snack.address = req.body.address;
        // save the bear and check for errors
        snack.save(function(err) {
            if (err)
                res.send(err);
            res.json({ message: 'Snack created!' });
        });   
    })

    .get(function(req, res) {
      Snack.find(function(err, snack) {
        if (err) {
         return res.send(err);
        }
        res.json(snack);
      }).sort( { loves: -1 } )
    });


router.route('/snack/:snack_id')
  .get(function(req,res){
    Snack.findById(req.params.snack_id,function(err,snack){
      if (err)
         res.send(err);
       res.json(snack);
    });
  })
  .put(function(req,res) {
    Snack.findById(req.params.snack_id,function(err,snack){
      if (err)
         res.send(err);
        snack.title = req.body.title; 
        snack.loves = req.body.loves;
        snack.description = req.body.description;
        snack.category = req.body.category;

        snack.save(function(err) {
            if (err)
                res.send(err);
            res.json({ message: 'Snack updated!' });
        });
    });
  })
  .delete(function(req,res) {
    Snack.remove({
      _id:req.params.snack_id
    },function(err,snack) {
      if (err)
        res.send(err);

       res.json({ message: 'Successfully deleted' });
    })
  });

router.route('/snack/addGood/:id')
  .put(function(req,res) {
    Snack.findById(req.params.id,function(err,snack){
      if (err)
         res.json({success: false});
        if( snack.loves){
        	   snack.loves = snack.loves + 1;
        }else{
        	snack.loves = 1;
        }
     
        snack.save(function(err) {
            if (err)
                res.json({success: false});
            res.json({ message: 'Snack loves updated!',success: true });
        });
    });
  });

router.route('/snack/search/:keyword')
.get(function(req, res) {
		var keyword = req.params.keyword;
		console.log(keyword);
		if(keyword=='all'){
			Snack.find(function(err, snack) {
		        if (err) {
		         return res.send(err);
		        }
		        res.json(snack);
		      }).sort( { loves: -1 } )
		}else{
		      Snack.find({ $or: [{ name: { $regex: keyword, $options: 'i' }},{ description: { $regex: keyword, $options: 'i' } },{ category: { $regex: keyword, $options: 'i' } }]},function(err, snack) {
		        if (err) {
		         return res.send(err);
		        }
		        res.json(snack);
		      }).sort( { loves: -1 } )
		}
    });
module.exports = router;